package com.gcd.gym.api;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gcd.gym.model.searchModel.CourseSearchModel;
import com.gcd.gym.model.searchModel.ReserveSearchModel;
import com.gcd.gym.service.CourseService;
import com.gcd.gym.utils.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.List;

@RestController
@RequestMapping(value = "/api/v1/course",produces = MediaType.APPLICATION_JSON_VALUE)
public class CourseApi {
    private CourseService courseService;

    @Autowired
    public void setCourseService(CourseService courseService) {
        this.courseService = courseService;
    }

    // 获取课程
    @GetMapping
    public ResponseEntity<JsonResult<?>> findCourse(@RequestParam(defaultValue = "1") Integer pageNo,
                                                    @RequestParam(defaultValue = "7") Integer pageSize,
                                                    CourseSearchModel course) {
        Page<CourseSearchModel> page = new Page<>(pageNo, pageSize);
        page=courseService.findCourse(page,course);
        return ResponseEntity.ok(JsonResult.success(page));
    }

    // 删除课程
    @DeleteMapping
    public ResponseEntity<JsonResult<?>> deleteCourse(@RequestBody Integer[] ids) {
        List<Integer> courseId= Arrays.asList(ids);
        int count =courseService.deleteCourseById(courseId);
        if (count==0) {
            return ResponseEntity.ok(JsonResult.fail("课程删除失败！"));
        } else {
            return ResponseEntity.ok(JsonResult.success(count));
        }
    }

// 添加课程
    @PostMapping
    public ResponseEntity<JsonResult<?>> addCourse(@RequestBody CourseSearchModel course) {
        boolean result= courseService.addCourse(course);
        if( result) {
            return ResponseEntity.ok(JsonResult.success(result));
        } else {
            return ResponseEntity.ok(JsonResult.fail("课程添加失败！"));
        }
    }

    // 修改课程
    @PutMapping
    public ResponseEntity<JsonResult<?>> updateCourse(@RequestBody CourseSearchModel course) {
        boolean result= courseService.updateCourse(course);
        if( result) {
            return ResponseEntity.ok(JsonResult.success(result));
        } else {
            return ResponseEntity.ok(JsonResult.fail("课程修改失败！"));
        }
    }


    // 获取课程的会员
    @GetMapping("/memberOfCourse")
    public ResponseEntity<JsonResult<?>> memberOfCourse(@RequestParam Integer courseId) {
        List<ReserveSearchModel> memberOfCourse= courseService.memberOfCourse(courseId);
        return ResponseEntity.ok(JsonResult.success(memberOfCourse));
    }

    // 签到
    @PostMapping("/checkIn")
    public ResponseEntity<JsonResult<?>> checkIn(@RequestBody ReserveSearchModel  reserve) {
        if (reserve.getCourseTime().isBefore(LocalDateTime.now())) {
            return ResponseEntity.ok(JsonResult.fail("签到时间已过！"));
        } else if (reserve.getCourseTime().minusMinutes(10).isAfter(LocalDateTime.now())) {
            return ResponseEntity.ok(JsonResult.fail("现在还未到签到时间，请在上课前10分钟签到！"));
        } else {
            boolean result= courseService.checkIn(reserve);
            if( result) {
                return ResponseEntity.ok(JsonResult.success(result));
            } else {
                return ResponseEntity.ok(JsonResult.fail("签到失败！"));
            }
        }
    }

    // 签退
    @PostMapping("/markAbsent")
    public ResponseEntity<JsonResult<?>> markAbsent(@RequestBody ReserveSearchModel  reserve) {
        if (reserve.getCourseTime().isAfter(LocalDateTime.now())) {
            return ResponseEntity.ok(JsonResult.fail("课程尚未开始，无法签退！！"));
        } else if (reserve.getCourseTime().isBefore(LocalDateTime.now())) {
            return ResponseEntity.ok(JsonResult.fail("课程已结束，无法签退！"));
        } else {
            boolean result= courseService.markAbsent(reserve);
            if( result) {
                return ResponseEntity.ok(JsonResult.success(result));
            } else {
                return ResponseEntity.ok(JsonResult.fail("签退失败！"));
            }
        }

    }
}
